package com.huyi.common.datasource;

import java.util.Map;

import javax.sql.DataSource;

import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;

import lombok.extern.slf4j.Slf4j;

/**
 * 多数据源管理
 * 
 * @author shengli.hu
 *
 */
@Slf4j
public class DynamicDataSource extends AbstractRoutingDataSource {
	
	/**
	 * 构造方法
	 * 
	 * @param defaultTargetDataSource
	 * @param targetDataSources
	 */
    public DynamicDataSource(DataSource defaultTargetDataSource, Map<Object, Object> targetDataSources) {
        super.setDefaultTargetDataSource(defaultTargetDataSource);
        super.setTargetDataSources(targetDataSources);
        super.afterPropertiesSet();
    }
	
	@Override
	protected Object determineCurrentLookupKey() {
		if(DataSourceContextHolder.getDataSourceType() != null) {
			log.info("当前数据源：{}", DataSourceContextHolder.getDataSourceType());
		}
		return DataSourceContextHolder.getDataSourceType();
	}

}
